package com.fate.system.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fate.framework.dto.IdParamDTO;
import com.fate.framework.utils.SystemUtil;
import com.fate.system.dto.tenant.TenantAddDTO;
import com.fate.system.dto.tenant.TenantPageDTO;
import com.fate.system.dto.tenant.TenantUpdateDTO;
import com.fate.system.entity.Tenant;
import com.fate.system.service.ITenantService;
import com.fate.system.vo.tenant.TenantDetailVO;
import com.fate.system.vo.tenant.TenantListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "租户操作 API")
@RestController
@RequestMapping("/api/v1.0/tenant")
public class TenantController {

  @Resource private ITenantService tenantService;

  @SaCheckRole("super_admin")
  @Operation(summary = "新增")
  @PostMapping("/add")
  public void add(@RequestBody @Valid TenantAddDTO addDTO) {
    Tenant tenant = SystemUtil.buildResource(addDTO, Tenant.class);
    tenantService.save(tenant);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "删除")
  @PostMapping("/remove")
  public void remove(@RequestBody @Valid IdParamDTO idParamDTO) {
    tenantService.removeById(idParamDTO.getId());
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "更新")
  @PostMapping("/update")
  public void update(@RequestBody @Valid TenantUpdateDTO updateDTO) {
    Tenant tenant = SystemUtil.buildResource(updateDTO, Tenant.class);
    tenantService.updateById(tenant);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "详情")
  @PostMapping("/detail")
  public TenantDetailVO detail(@RequestBody @Valid IdParamDTO idParamDTO) {
    Tenant tenant = tenantService.getById(idParamDTO.getId());
    return SystemUtil.buildResource(tenant, TenantDetailVO.class);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "分页列表")
  @PostMapping("/getPageList")
  public Page<TenantListVO> getPageList(@RequestBody @Valid TenantPageDTO pageDTO) {
    Page<Tenant> page = tenantService.getPageList(pageDTO);
    return SystemUtil.buildResource(page, TenantListVO.class);
  }
}
